package YunNotes.servlet;


import YunNotes.allnotes.dao.PublicSquareAll;
import YunNotes.allnotes.dao.UserPublicNotes;
import YunNotes.util.json.ChangeJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * Servlet类
 *
 * @author 10593
 */

@WebServlet("/UserPublicServlet")
public class UserPublicServlet extends HttpServlet {
    /**
     * doPost方法：
     * 从接口请求中获得id参数，和p_headline参数,输出对应用户的公开笔记。
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 响应参数格式设置
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //向外传值,并将数据转化为json格式保存在一个字符串对象里
        PublicSquareAll publicSquareAll = new PublicSquareAll();
        ChangeJson changeJson = new ChangeJson();

        PrintWriter out = response.getWriter();

//        //从前端的请求头中获得token并且于数据库中的token做对比获得用户的id
//        String token = request.getHeader("token");
//        TokenJudge tokenJudge = new TokenJudge();
//        Integer id = 0;
//
//        int idtrue = 0;
//
//        try {
//            id = tokenJudge.tokenJudge(token);
//        }catch (SQLException e){
//            e.printStackTrace();;
//        }
//
//
//        if(id == 0 ){
//            out.println("您的token有误");
//        }else{
//            idtrue = id;
//        }

//        out.println("用户id为："+idtrue);


        //获取前端的请求的方法
        String requestMethod = request.getMethod();
        System.out.println("前端的请求方法：" + requestMethod);


        //通过中间变量的形式从前端获取笔记序号（num）并将其转化为int类型
        String middle = request.getParameter("num");
        int num = Integer.parseInt(middle);

        //输出对应笔记
        UserPublicNotes userPublicNotes = new UserPublicNotes();
        String str4 = changeJson.ListToJson(userPublicNotes.outputUserPublicNotes(num));
        //将此字符串对象输出
        out.println(str4);
        //给后台一个反馈信息
        System.out.println(str4);

    }

    /**
     * doGet方法：
     * 转到doPost方法。
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
